package com.fick.struts.action;

import org.apache.struts.actions.DispatchAction;

import com.fick.hibernate.dao.*;
import com.fick.hibernate.model.*;
import com.fick.tools.SystemLogger;
import com.fick.util.General;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class ManageJobclassAction extends DispatchAction{
	
	private static final Log log = LogFactory.getLog(ManageJobclassAction.class);
	//修改招聘分类
	public ActionForward upJc(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String jcid = request.getParameter("jcid");
		String jcname = request.getParameter("jcname");
		
		try{
			Jobclass jc = new Jobclass();
			JobclassDAO jcDAO = new JobclassDAO();
			int id = Integer.parseInt(jcid);
			jc = jcDAO.findById(id);
			
			jc.setName(jcname);
			jcDAO.update(jc);
			request.setAttribute("message", "修改分类成功");
			return mapping.findForward("toList");
		}catch(Exception e)
		{
			e.printStackTrace();
			log.error(e);
			SystemLogger.errorlog("1", "职位分类", "修改职位分类失败");
			return null;
		}
	}
	//添加招聘分类
	public  ActionForward addJc(ActionMapping mapping,ActionForm form,HttpServletRequest request,
				HttpServletResponse response)
	{
		String jcname = request.getParameter("jcname");
		try{
			Jobclass jc = new Jobclass();
			JobclassDAO jcDAO = new JobclassDAO();
			
			jc.setName(jcname);
			jcDAO.save(jc);
			
			request.setAttribute("message", "添加分类成功");
			return mapping.findForward("toList");
			
		}catch(Exception e)
		{
			e.printStackTrace();
			log.error(e);
			SystemLogger.errorlog("1", "职位分类", "添加职位分类失败");
			return null;
		}
	}
	//删除招聘分类 单条
	public ActionForward delById(ActionMapping mapping,ActionForm form,HttpServletRequest request,
				HttpServletResponse response)
	{
		String jcid = request.getParameter("jcid");
		try{
			Jobclass jc = new Jobclass();
			JobclassDAO jcDAO = new JobclassDAO();
			
			jc = jcDAO.findById(Integer.parseInt(jcid));
			jcDAO.delete(jc);
			
			request.setAttribute("message","删除分类成功");
			return mapping.findForward("toList");
		}catch(Exception e)
		{
			
			e.printStackTrace();
			log.error(e);
			SystemLogger.errorlog("1", "职位分类", "删除职位分类失败");
			return null;
		}
	}
	//删除招聘分类 多条
	public ActionForward delJcByStr(ActionMapping mapping,ActionForm form,HttpServletRequest request,
			HttpServletResponse response)
	{
		SystemLogger.operatelog(request, "删除多个新闻");
		
		String str = request.getParameter("str");
		JobclassDAO jcDAO = new JobclassDAO();
		Jobclass jc = new Jobclass();
		String[] ids = str.split(",");
		int id = 0;
		try {
			for (int i = 0; i < ids.length; i++) {
				id = Integer.parseInt(ids[i]);
				jc = jcDAO.findById(id);
				jcDAO.delete(jc);
			}
			request.setAttribute("message","删除多条职位成功");
			return mapping.findForward("toList");
		} catch (RuntimeException e) {
			e.printStackTrace();
			log.error(e);
			SystemLogger.errorlog("1", "职位分类", "删除职位失败");
			return null;
		}
	}
}
